package com.gitee.vincentzyu.qwq_forge189.client.gui;

import com.gitee.vincentzyu.qwq_forge189.QwqForge189;
import com.gitee.vincentzyu.qwq_forge189.common.EventLoader;
import com.gitee.vincentzyu.qwq_forge189.inventory.AchievementContainer;
import com.gitee.vincentzyu.qwq_forge189.inventory.ContainerDemo;
import com.gitee.vincentzyu.qwq_forge189.item.ItemLoader;
import ibxm.Player;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.Achievement;
import net.minecraft.stats.AchievementList;
import net.minecraft.stats.StatisticsFile;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import net.minecraft.client.gui.achievement.GuiAchievements;

import javax.swing.text.html.parser.Entity;
import java.util.stream.IntStream;

@SideOnly(Side.CLIENT)
public class GuiAchievementContainer extends GuiContainer {

    private static final String TEXTURE_PATH = QwqForge189.MODID + ":" + "textures/gui/container/achievement_gui.png";
    private static final ResourceLocation TEXTURE = new ResourceLocation(TEXTURE_PATH);
    private static final int[] BUTTON_IDX = new int[36];

    private static final int BUTTON_PRE = 100;

    private static final int BUTTON_NEXT = 101;

    private static int currentPage = 0;


    static {
        for ( int i=0; i < BUTTON_IDX.length; i++ ){
            BUTTON_IDX[i] = i;
        }
    }

    public GuiAchievementContainer(AchievementContainer inventorySlotIn){
        super(inventorySlotIn);
        this.xSize = 255;
        this.ySize = 133;
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
        GlStateManager.color(1.0F, 1.0F, 1.0F);

        this.mc.getTextureManager().bindTexture(TEXTURE);
        int offsetX = (this.width - this.xSize) / 2, offsetY = (this.height - this.ySize) / 2;

        this.drawTexturedModalRect(offsetX, offsetY, 0, 0, this.xSize, this.ySize);
    }

    @Override
    protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
        this.drawHorizontalLine(10, 245, 20, 0xFF000000);

        String title = I18n.format("container.qwq_forge189.achievement_gui");
        this.fontRendererObj.drawString(
            title,
            (this.xSize -  this.fontRendererObj.getStringWidth(title)) / 2, 6, 0x404040
        );

        ItemStack item = new ItemStack(ItemLoader.qwqItem);
        this.itemRender.renderItemAndEffectIntoGUI(item, 5, 5);
    }

    @Override
    public void initGui(){
        super.initGui();
        int offsetX = (this.width - this.xSize) / 2, offsetY = (this.height - this.ySize) / 2;

        this.buttonList.add(
            new GuiButton(BUTTON_PRE, offsetX+200, offsetY+30, 20, 15, "" ){
                @Override
                public void drawButton(Minecraft mc, int mouseX, int mouseY) {
                    if ( this.visible ){
                        GlStateManager.color(1.0F, 1.0F, 1.0F);

                        mc.getTextureManager().bindTexture(TEXTURE);
                        int x = mouseX - this.xPosition, y = mouseY - this.yPosition;

                        if (x >= 0 && y >= 0 && x < this.width && y < this.height) {
                            this.drawTexturedModalRect(this.xPosition, this.yPosition, 32, 226, this.width, this.height);
                        } else {
                            this.drawTexturedModalRect(this.xPosition, this.yPosition, 44, 226, this.width, this.height);
                        }
                    }
                }
            }
        );

        this.buttonList.add(
                new GuiButton(BUTTON_NEXT, offsetX+400, offsetY+30, 20, 15, "" ){
                    @Override
                    public void drawButton(Minecraft mc, int mouseX, int mouseY) {
                        if ( this.visible ){
                            GlStateManager.color(1.0F, 1.0F, 1.0F);

                            mc.getTextureManager().bindTexture(TEXTURE);
                            int x = mouseX - this.xPosition, y = mouseY - this.yPosition;

                            if (x >= 0 && y >= 0 && x < this.width && y < this.height) {
                                this.drawTexturedModalRect(this.xPosition, this.yPosition, 32, 207, this.width, this.height);
                            } else {
                                this.drawTexturedModalRect(this.xPosition, this.yPosition, 44, 207, this.width, this.height);
                            }
                        }
                    }
                }
        );


        for ( int i=0; i < 5; i++ ){
            int idx = BUTTON_IDX[i];
            Achievement achievement = AchievementList.achievementList.get(i);
            AchievementContainer achievementContainer = (AchievementContainer) this.inventorySlots;
            EntityPlayerMP player = (EntityPlayerMP) achievementContainer.player;
//            EntityPlayer player = achievementContainer.player;
            StatisticsFile statisticsFile = achievementContainer.statisticsFile;
//            player.getCommandStats()

            String str1 = EnumChatFormatting.GRAY + "成就名: " + EnumChatFormatting.WHITE + achievement.getStatName().getUnformattedText();
            String str2 = EnumChatFormatting.GRAY + "完成状态: ";
            String str3 = statisticsFile.hasAchievementUnlocked(achievement) ? EnumChatFormatting.GREEN + "已经完成" : "还未完成";
            String achievement_text = str1 + ", " + str2 + str3;
            this.buttonList.add(
                new GuiButton(
                        idx,
                        offsetX+25,
                        offsetY+15+idx*22,
                        200,
                        20,
                        achievement_text
                )
            );
        }



    }



}
